import cv2

img1 = cv2.imread('img3.jpg')
img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
width, height = img2.shape
blurred = cv2.GaussianBlur(img2, (5, 5), 0)
retval, dst = cv2.threshold(blurred, 73, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
    area_list = []
    print(len(contours))
    for i in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        if (width / 2 < w) or (height / 2 < h):  # 面积过小的不框选
            pass
        else:
            area = cv2.contourArea(contours[i])
            area_list.append([area, i])
            area_list.sort(reverse=True)
            #cv2.drawContours(img1, contours[i], -1, (0, 255, 0), 1)
    pos = []

    for u in range(8):  # 八个定位点
        #cv2.drawContours(img1, contours[area_list[u][1]], -1, (0, 255, 0), 1)
        x, y, w, h = cv2.boundingRect(contours[area_list[u][1]])
        #print(x,y,w,h)
        pos.append([x, y, w, h])

    def eight_pos(a):
        l = len(a) #排序算法
        for i in range(l):
            for j in range(l):
                if (a[i][0] < a[j][0]):
                    a[i], a[j] = a[j], a[i]
        for u in range(0, l, 2):
            if (a[u][1] > a[u + 1][1]):
                a[u], a[u + 1] = a[u + 1], a[u]
        return a

    pos=eight_pos(pos)
    #print(pos)
    # print(chang, kuan)
    #这是三大块
    # cv2.rectangle(img1, (pos[0][0] + pos[0][3], pos[0][1] + pos[0][2]), (pos[3][0] + pos[3][2], pos[3][1]),
    #               (255, 255, 0), 2)
    # cv2.rectangle(img1, (pos[2][0] + pos[2][3], pos[2][1] + pos[2][2]), (pos[5][0] + pos[5][2], pos[5][1]),
    #               (255, 255, 0), 2)
    # cv2.rectangle(img1, (pos[4][0] + pos[4][3], pos[4][1] + pos[4][2]), (pos[7][0] + pos[7][2], pos[7][1]),
    #               (255, 255, 0),
    #               2)
    dingwei=[]
    #for U in range(9,26):  # 八个定位点 左侧的17个定位点 第8个是填涂实例不需要这个。
    U=9
    ran=9
    while U <26 and 0< ran <len(contours):#U搜索轮廓下标 ran进行计数
        x, y, w, h = cv2.boundingRect(contours[area_list[ran][1]])
        if x<pos[0][0]+pos[0][2]:
            #cv2.drawContours(img1, contours[area_list[ran][1]], -1, (255, 255, 0), 1)
            U+=1
        # print(x,y,w,h)
            dingwei.append([x,y,w,h])
        ran+=1
    dingwei.sort( key=lambda dingwei : dingwei[1])
    # #print(dingwei[-1])
    #print(dingwei)
    #第三题：
    #21
    cv2.rectangle(img1, (pos[0][0]+pos[0][2], dingwei[-1][1] + pos[0][2]), (pos[3][0] + pos[3][2], pos[3][1]),
                                  (255, 255, 0), 2)
    t21_1=img1[ dingwei[-1][1] + pos[0][2]:pos[3][1],dingwei[-1][0] + pos[0][3]:pos[3][0] + pos[3][2]]
    cv2.rectangle(img1,(pos[2][0] + pos[2][3], pos[2][1] + pos[2][2]),(pos[5][0]+pos[5][2] , dingwei[0][1]-dingwei[0][2]),(255, 255, 0), 2)
    t21_2 =img1[pos[2][1] + pos[2][2]:dingwei[0][1]-dingwei[0][2],pos[2][0] + pos[2][3]:pos[5][0]]
    cv2.imwrite('21_1.jpg',t21_1)
    cv2.imwrite('21_2.jpg', t21_2)
   # # ---
   # #  #22
    re_x, re_y, re_w, re_h = cv2.boundingRect(contours[area_list[8][1]])
    cv2.rectangle(img1, (pos[2][0] + 2*pos[2][3], dingwei[0][1]-dingwei[0][3]), (pos[5][0]+pos[5][2],re_y+re_h),
                 (255, 255, 0), 2)
    t22 = img1[dingwei[0][1]-dingwei[0][3]:re_y+re_h, pos[2][0] + 2*pos[2][3]:pos[5][0]]
    cv2.imwrite('22.jpg', t22)
   #  ## ---
   # #  #23
    cv2.rectangle(img1, (pos[2][0] + 2 * pos[2][3], re_y + re_h+re_h), (pos[5][0]+pos[5][2], dingwei[-2][1]-dingwei[-2][3]),
                  (255, 255, 0), 2)
    t23 = img1[re_y + re_h+re_h:dingwei[-2][1],pos[2][0] + 2 * pos[2][3]:pos[5][0]]
    cv2.imwrite('23.jpg', t23)
   # #  # ---
   # # # 24
    cv2.rectangle(img1, (pos[2][0] + 2 * pos[2][3], dingwei[-2][1]), (pos[5][0], pos[5][1]),
                  (255, 255, 0), 2)
    cv2.rectangle(img1, (pos[4][0] + pos[4][3], pos[4][1] + pos[4][2]), (pos[6][0] + pos[6][3], dingwei[0][1]+2*dingwei[0][3]),
                  (255, 255, 0), 2)
    t24_1 = img1[dingwei[-2][1]:pos[5][1],pos[2][0] + 2 * pos[2][3]:pos[5][0]]
    cv2.imwrite('24_1.jpg', t24_1)
    t24_2 = img1[pos[4][1] + pos[4][2]:dingwei[0][1]+2*dingwei[0][3],pos[4][0] + pos[4][3]:pos[6][0] + pos[6][3]]
    cv2.imwrite('24_2.jpg', t24_2)
   # #
   # # # ---
   # # 25
    cv2.rectangle(img1, (pos[4][0] + pos[4][3], dingwei[2][1]),
                  (pos[6][0] + pos[6][3], dingwei[-3][1] ),
                  (255, 255, 0), 2)
    t25 = img1[dingwei[2][1]:dingwei[13][1],pos[4][0] + pos[4][3]:pos[6][0] + pos[6][3]]
    cv2.imwrite('25.jpg', t25)

#---

    choice_dingwei = []
    #for X in range(26, 46):  # 顶部二十个定位点。
    X=26
    cho=0
    while X <46 and cho <len(contours)-1:
        x, y, w, h = cv2.boundingRect(contours[area_list[cho][1]])
        if pos[0][0]<x<pos[2][0] and dingwei[0][1]-3*dingwei[0][3]>y:
            #cv2.drawContours(img1, contours[area_list[cho][1]], -1, (0, 255, 0), 1)
            X+=1
            choice_dingwei.append([x, y, w, h])
        # print(x,y,w,h)

        cho+=1
    # print('x:',str(X))
    # print('cho:', str(cho))
    # print(choice_dingwei)

    choice_dingwei.sort(key=lambda choice_dingwei: choice_dingwei[0])
    # print(choice_dingwei)

    choice_answer=[]
    choice_result = []
    for M in range(len(contours)-1):
        x, y, w, h = cv2.boundingRect(contours[area_list[M][1]])
        if choice_dingwei[0][0]+choice_dingwei[0][2]<=x<choice_dingwei[16][0] + choice_dingwei[16][2] and y<=dingwei[16][1] + dingwei[16][3]  and y >=dingwei[10][1]:
            #cv2.drawContours(img1, contours[area_list[M][1]], -1, (255, 255, 255), 3)
            choice_answer.append([x, y, w, h,M])
    print(len(choice_answer))
    #print(choice_answer)

    # ----------------------------------------
    # 选择题判题 先按题号划分再识别ABCD

    # cho1 = []
    # cho2 = []
    # cho3 = []
    # cho4 = []
    # cho5 = []
    # cho6 = []
    # cho7 = []
    # for C in range(len(choice_answer)):
    #     if choice_answer[C][1]<=dingwei[10][1]+dingwei[10][3]:
    #         cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #         cho1.append(C)
    #     elif choice_answer[C][1]<=dingwei[11][1]+dingwei[11][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho2.append(C)
    #     elif choice_answer[C][1]<=dingwei[12][1]+dingwei[12][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho3.append(C)
    #     elif choice_answer[C][1]<=dingwei[13][1]+dingwei[13][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho4.append(C)
    #     elif choice_answer[C][1]<=dingwei[14][1]+dingwei[14][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho5.append(C)
    #     elif choice_answer[C][1]<=dingwei[15][1]+dingwei[15][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho6.append(C)
    #     elif choice_answer[C][1]<=dingwei[16][1]+dingwei[16][3]:
    #          cv2.drawContours(img1, contours[area_list[choice_answer[C][4]][1]], -1, (0, 255, 0), 1)
    #          cho7.append(C)
    #     else:
    #         pass
    # print(cho1)
    # print(cho2)

    # 1.

    # ----------------------------------------

    def cho_ans_1_17(ans,choice_dingwei):
        if choice_answer[ans][0] <= choice_dingwei[1][0] + choice_dingwei[1][2]:  # A
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'A'
        elif choice_answer[ans][0] <= choice_dingwei[2][0] + choice_dingwei[2][2]:  # B
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'B'
        elif choice_answer[ans][0] <= choice_dingwei[3][0] + choice_dingwei[3][2]:  # C
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'C'
        elif choice_answer[ans][0] <= choice_dingwei[4][0] + choice_dingwei[4][2]:  # D
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'D'

    def cho_ans_6_19(ans,choice_dingwei):
        if choice_dingwei[7][0] <= choice_answer[ans][0] <= choice_dingwei[7][0] + choice_dingwei[7][2]:  # A
           # cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'A'
        elif choice_answer[ans][0] <= choice_dingwei[8][0] + choice_dingwei[8][2]:  # B
           # cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'B'
        elif choice_answer[ans][0] <= choice_dingwei[9][0] + choice_dingwei[9][2]:  # C
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'C'
        elif choice_answer[ans][0] <= choice_dingwei[10][0] + choice_dingwei[10][2]:  # D
           # cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'D'

    def cho_ans_11_20(ans,choice_dingwei):
        if choice_dingwei[13][0] <= choice_answer[ans][0] <= choice_dingwei[13][0] + choice_dingwei[13][2]:  # A
           # cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'A'
        elif choice_answer[ans][0] <= choice_dingwei[14][0] + choice_dingwei[14][2]:  # B
          #  cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'B'
        elif choice_answer[ans][0] <= choice_dingwei[15][0] + choice_dingwei[15][2]:  # C
            #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return  'C'
        elif choice_answer[ans][0] <= choice_dingwei[16][0] + choice_dingwei[16][2]:  # D
          #  cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (255, 255, 0), 1)
            return 'D'

    pri_res=[]
    for ans in range(len(choice_answer)):
        if choice_dingwei[1][0] <=choice_answer[ans][0]<=choice_dingwei[5][0]:
            #cv2.drawContours(img1,contours[area_list[choice_answer[ans][4]][1]],-1,(0,255,0),1)
            if choice_answer[ans][1]<=dingwei[10][1]+dingwei[10][3]:#第一题
                # cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                #flag=0#判断这个选择题涂了几个
                print('1 ',cho_ans_1_17(ans, choice_dingwei))
            elif choice_answer[ans][1]<=dingwei[11][1]+dingwei[11][3]:#第二题
                #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                print('2 ', cho_ans_1_17(ans, choice_dingwei))
            elif choice_answer[ans][1]<=dingwei[12][1]+dingwei[12][3]:#第三题
                #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                print('3 ', cho_ans_1_17(ans, choice_dingwei))
            elif choice_answer[ans][1]<=dingwei[13][1]+dingwei[13][3]:#第四题
                #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                print('4 ', cho_ans_1_17(ans, choice_dingwei))
            elif choice_answer[ans][1]<=dingwei[14][1]+dingwei[14][3]:#第五题
            #    cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                print('5 ', cho_ans_1_17(ans, choice_dingwei))
            elif dingwei[15][1]- 2*dingwei[15][3]<choice_answer[ans][1] <= dingwei[15][1] + dingwei[15][3]:  # 第16题
                #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
                print('16 ', cho_ans_1_17(ans, choice_dingwei))

            elif choice_answer[ans][1] <= dingwei[16][1] + dingwei[16][3]:  # 第17题
                print('17 ', cho_ans_1_17(ans, choice_dingwei))
                #cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)
        elif choice_dingwei[7][0] < choice_answer[ans][0] <= choice_dingwei[11][0]:
            #cv2.drawContours(img1,contours[area_list[choice_answer[ans][4]][1]],-1,(0,255,0),1)
            if choice_answer[ans][1] <= dingwei[10][1] + dingwei[10][3]:  # 第六题
                print('6 ' + cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[11][1] + dingwei[11][3]:  # 第七题
                print('7 '+cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[12][1] + dingwei[12][3]:  # 第八题
                print('8 ' + cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[13][1] + dingwei[13][3]:  # 第九题
                print('9 ' + cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[14][1] + dingwei[14][3]:  # 第十题
                print('10 ' + cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[15][1] + dingwei[15][3]:  # 第18题
                print('18 ' + cho_ans_6_19(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[16][1] + dingwei[16][3]:  # 第19题
                print('19 ' + cho_ans_6_19(ans, choice_dingwei))
        elif choice_dingwei[13][0] < choice_answer[ans][0] <= choice_dingwei[17][0]:
            #cv2.drawContours(img1,contours[area_list[choice_answer[ans][4]][1]],-1,(0,255,0),1)
            if choice_answer[ans][1] <= dingwei[10][1] + dingwei[10][3]:#11
                print('11 '+cho_ans_11_20(ans, choice_dingwei))

            elif choice_answer[ans][1] <= dingwei[11][1] + dingwei[11][3]:#12
                cv2.drawContours(img1, contours[area_list[choice_answer[ans][4]][1]], -1, (0, 255, 0), 1)

                print('12 ' + cho_ans_11_20(ans, choice_dingwei))

            elif choice_answer[ans][1] <= dingwei[12][1] + dingwei[12][3]:#13
                print('13 ' + cho_ans_11_20(ans, choice_dingwei))
            elif choice_answer[ans][1] <= dingwei[13][1] + dingwei[13][3]:#14
                print('14 ' + cho_ans_11_20(ans, choice_dingwei))

            elif choice_answer[ans][1] <= dingwei[14][1] + dingwei[14][3]:#15
                print('15 ' + cho_ans_11_20(ans, choice_dingwei))
                cv2.drawContours(img1,contours[area_list[choice_answer[ans][4]][1]],-1,(0,255,0),1)
            elif choice_answer[ans][1] <= dingwei[15][1] + dingwei[14][3]:#20
                print('20 '+cho_ans_11_20(ans, choice_dingwei))

fx = 0.6
fy = 0.6
img1 = cv2.resize(img1, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC)
dst = cv2.resize(dst, (0, 0), fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC)
cv2.imshow('img1', img1)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
